<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\StoreArticleRequest;
use App\Http\Controllers\Controller;
use DB;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // 列表页
    public function index()
    {
        //
        $title = '文章列表';
        $data = DB::table('article') -> get();
        return view('admin.article.index',['title' => $title,'data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    // add添加
    public function create()
    {
        //
        $title = '文章添加';
        return view('admin.article.add',['title' => $title]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    // 执行add  执行添加页
    public function store(StoreArticleRequest $request)
    {
        //删除token
        $data = $request -> except('_token');
        //处理图片
        if($request -> hasFile('img'))
        {
            if($request -> file('img') -> isValid())
            {
                //获取扩展名
                $suffix = $request -> file('img') -> getClientOriginalExtension();
                // echo $suffix;
                //随机生成文件名称
                $fileName = time() . mt_rand(100000,999999).'.'.$suffix;
                // echo $fileName;

                $path = date('Ymd');
                $data['img'] = '/uploads/'.$path.'/'.$fileName;

                //移动文件
                $request -> file('img') -> move('./uploads/'.$path,$fileName);
            }
        }
        //处理发布时间字段
        $data['create_at'] = time();
        //处理更行时间字段
        $data['update_at'] = time(); 
        // dd($data);

        $res = DB::table('article') -> insert($data);
        // dd($res);
        if($res)
        {
            return redirect('/admin/article') -> with(['success' => '添加成功']);
        }else
        {
            return back() -> with(['error' => '添加失败']);
        }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // 详情页
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // 编辑表单页
    public function edit($id)
    {
        //
        $title = '文章编辑';
        $data = DB::table('article') -> where('id',$id) -> first();
        return view('admin.article.edit',['title' => $title,'data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // 执行更新页面
    public function update(Request $request, $id)
    {
        // return dd($request -> all());
        $data = $request -> except(['_token','_method','oldimg']);
        $oldimg = '.'.$request -> input('oldimg');

         //处理图片
        if($request -> hasFile('img'))
        {
            if($request -> file('img') -> isValid())
            {
                //获取扩展名
                $suffix = $request -> file('img') -> getClientOriginalExtension();
                // echo $suffix;
                //随机生成文件名称
                $fileName = time() . mt_rand(100000,999999).'.'.$suffix;
                // echo $fileName;

                $path = date('Ymd');
                $data['img'] = '/uploads/'.$path.'/'.$fileName;

                //移动文件
                $request -> file('img') -> move('./uploads/'.$path,$fileName);
                if($oldimg != '.')
                {
                    if(file_exists($oldimg))
                    {
                        unlink($oldimg);
                    }
                }
                
            }
        }

        //处理更新时间
        $data['update_at'] = time();

        // dd($data);
        $res = DB::table('article') -> where('id',$id) -> update($data);
        if($res)
        {
            return redirect('/admin/article') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // 删除
    public function destroy($id)
    {
        // dd($data);
        // 获取就图片路径
        $path = '.'.DB::table('article') -> select('img') -> where('id',$id) -> first() -> img;

        $res = DB::table('article') -> where('id',$id) -> delete();
        if($res)
        {
            if($path != '.')
                {
                    if(file_exists($path))
                    {
                        unlink($path);
                    }
                }
            return redirect('/admin/article') -> with(['success' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }
}
